<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Image Detections</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 20px;
        }
        h1 {
            color: #333;
        }
        table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 20px;
        }
        th, td {
            border: 1px solid #ddd;
            padding: 8px;
            text-align: left;
        }
        th {
            background-color: #f2f2f2;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .form-container {
            margin-top: 30px;
            padding: 20px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #f5f5f5;
        }
        .form-group {
            margin-bottom: 15px;
        }
        label {
            display: inline-block;
            width: 100px;
            font-weight: bold;
        }
        input[type="text"], input[type="number"] {
            padding: 8px;
            width: 200px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
        button {
            padding: 8px 15px;
            background-color: #4CAF50;
            color: white;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            margin-right: 5px;
        }
        button.delete {
            background-color: #f44336;
        }
        button.cancel {
            background-color: #cccccc;
        }
        button:hover {
            opacity: 0.8;
        }
    </style>
</head>
<body>
<h1>Image Detection Results</h1>

<table>
    <thead>
    <tr>
        <th>ID</th>
        <th>Image Name</th>
        <th>X1</th>
        <th>Y1</th>
        <th>X2</th>
        <th>Y2</th>
        <th>Actions</th>
    </tr>
    </thead>
    <tbody>
    <tr th:each="detection : ${detections}">
        <td th:text="${detection.id}"></td>
        <td th:text="${detection.imageName}"></td>
        <td th:text="${detection.x1}"></td>
        <td th:text="${detection.y1}"></td>
        <td th:text="${detection.x2}"></td>
        <td th:text="${detection.y2}"></td>
        <td>
            <button class="edit-btn"
                    th:data-id="${detection.id}"
                    th:data-image="${detection.imageName}"
                    th:data-x1="${detection.x1}"
                    th:data-y1="${detection.y1}"
                    th:data-x2="${detection.x2}"
                    th:data-y2="${detection.y2}">
                Edit
            </button>

            <form th:action="@{'/detections/delete/' + ${detection.id}}" method="post" style="display: inline;">
                <button type="submit" class="delete">Delete</button>
            </form>
        </td>
    </tr>
    </tbody>
</table>

<div id="updateForm" class="form-container" style="display: none;">
    <h2>Update Detection</h2>
    <form id="updateDetectionForm" method="post">
        <input type="hidden" id="updateId" name="id">

        <div class="form-group">
            <label for="updateImageName">Image Name:</label>
            <input type="text" id="updateImageName" name="imageName" required>
        </div>

        <div class="form-group">
            <label for="updateX1">X1:</label>
            <input type="number" step="0.01" id="updateX1" name="x1" required>
        </div>

        <div class="form-group">
            <label for="updateY1">Y1:</label>
            <input type="number" step="0.01" id="updateY1" name="y1" required>
        </div>

        <div class="form-group">
            <label for="updateX2">X2:</label>
            <input type="number" step="0.01" id="updateX2" name="x2" required>
        </div>

        <div class="form-group">
            <label for="updateY2">Y2:</label>
            <input type="number" step="0.01" id="updateY2" name="y2" required>
        </div>

        <button type="submit">Update</button>
        <button type="button" onclick="hideUpdateForm()" class="cancel">Cancel</button>
    </form>
</div>

<script>
    document.addEventListener('DOMContentLoaded', function() {
        // 处理编辑按钮点击
        document.body.addEventListener('click', function(e) {
            if (e.target.classList.contains('edit-btn')) {
                const btn = e.target;
                showUpdateForm(
                    btn.getAttribute('data-id'),
                    btn.getAttribute('data-image'),
                    btn.getAttribute('data-x1'),
                    btn.getAttribute('data-y1'),
                    btn.getAttribute('data-x2'),
                    btn.getAttribute('data-y2')
                );
            }
        });

        // 处理表单提交
        document.getElementById('updateDetectionForm').addEventListener('submit', function(e) {
            e.preventDefault();
            this.submit();
        });
    });

    function showUpdateForm(id, imageName, x1, y1, x2, y2) {
        document.getElementById('updateId').value = id;
        document.getElementById('updateImageName').value = imageName;
        document.getElementById('updateX1').value = x1;
        document.getElementById('updateY1').value = y1;
        document.getElementById('updateX2').value = x2;
        document.getElementById('updateY2').value = y2;

        const form = document.getElementById('updateDetectionForm');
        form.action = '/detections/update/' + id;

        document.getElementById('updateForm').style.display = 'block';
        // 滚动到表单位置
        document.getElementById('updateForm').scrollIntoView({ behavior: 'smooth' });
    }

    function hideUpdateForm() {
        document.getElementById('updateForm').style.display = 'none';
    }
</script>
</body>
</html>